<template>
    <div class="table">
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="旧密码" prop="oldPass" required="true">
                <el-input type="password" v-model.number="ruleForm.oldPass" autocomplete="off" show-password="true"></el-input>
            </el-form-item>
            <el-form-item label="新密码" prop="newPass" required="true">
                <el-input type="password" v-model.number="ruleForm.newPass" autocomplete="off" show-password="true"></el-input>
            </el-form-item>
            <el-form-item label="确认新密码" prop="checkPass" required="true">
                <el-input type="password" v-model.number="ruleForm.checkPass" autocomplete="off" show-password="true"></el-input>
            </el-form-item>
            <el-form-item class="button">
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    // 组件名称
    name: 'passwordView',
    data() {
        var validateOldPass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入旧密码'));
            } else {
                console.log(value);
                callback();
            }
        };
        var validateNewPass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.checkPass !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入新密码'));
            } else if (value !== this.ruleForm.newPass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            ruleForm: {
                // 修改密码
                oldPass:'',
                newPass:'',
                checkPass:'',
            },
            rules: {
                oldPass: [
                    { validator: validateOldPass, trigger: 'blur' }
                ],
                newPass: [
                    { validator: validateNewPass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        };
    },
    methods: {
        submitForm(formName) {
            console.log(this.ruleForm);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    alert('submit!');
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }

}
</script> 

<style scoped lang='less'>
.table {
    width: 25%;
    margin: 15% auto;
}
</style>
